package org.cttic.visualization.visualization.z_service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cttic.visualization.visualization.common.*;
import org.cttic.visualization.visualization.topology.service.GraphDistanceDijkstra;
import org.cttic.visualization.visualization.util.*;
import org.cttic.visualization.visualization.util.routeIdentificationQuery.routeQueryUtil;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.cttic.visualization.visualization.z_service.z_RouteIdentificationQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.lang.model.element.VariableElement;
import java.net.URI;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2020-05-08 16:47
 * 2.3.3车辆途径识别点查询接口
 */
@Service
public class z_RouteIdentificationQueryImpl implements z_RouteIdentificationQuery {
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private chargingUtil chargingUtil;
    @Autowired
    private ESQueryUtil esQueryUtil;
    @Autowired
    private routeQueryUtil routeQueryUtil;
    @Override
    public String z_routeIdentificationQuery(String jsonData) {
        JSONObject json = ParseJsonDataUtil.getJsonData(jsonData);
        String   provinceId = json.getString("provinceId");
        String vehPlate = json.getString("vehPlate");
        String vehPlateColor = json.getString("vehPlateColor");
        String queryType = json.getString("queryType");
        String feeFlag = json.getString("feeFlag");     //计费查询标志
        String queryValue1 = json.getString("queryValue1");
//        if(!json.getString("queryValue1").contains("|")){
//            queryValue1 = json.getString("queryValue1");
//        }else if (json.getString("queryValue1").contains("|")){
//            String[] split = json.getString("queryValue1").split("\\|");
//            queryValue1=split[split.length-1];
//        }
        RouteIdentificationData rtData = new RouteIdentificationData();
        List<RouteIdentificationData.Identification> list= new ArrayList<>();

        if (queryType.equals("2")) {
            routeQueryUtil.routeQuery(provinceId,vehPlate,vehPlateColor,queryValue1,list,rtData,feeFlag);
            rtData.setEndLatArray(list);
            ObjectMapper mapper = new ObjectMapper();
            try {
                String value = mapper.writeValueAsString(rtData);
                return value;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
//            List<EsDataStrip> travelData = employeeRepository.findByVehicleNoAndVehicleColorAndTravelId(vehPlate, vehPlateColor, queryValue1);
//            if(travelData!=null&&travelData.size()!=0){
//                rtData.setIsOnline("1");
//                List<EsDataStrip> sortList = travelData.stream().sorted(Comparator.comparing(EsDataStrip::getTansTime)).collect(Collectors.toList());
//                String startGantryId = sortList.get(0).getGantryId();
//                String endGantryId = sortList.get(sortList.size() - 1).getGantryId();
//
//                for (EsDataStrip travel : travelData) {
//                    RouteIdentificationData.Identification identification = new RouteIdentificationData.Identification();
//                    identification.setTransTime(travel.getTansTime());
//                    identification.setEndLat(travel.getTravelId());
//                    identification.setVehStatus("1");
//                    int pointType = travel.getGantryType();
//                    identification.setPointType(pointType+"");
//                    String gantryId = travel.getGantryId();
//                    identification.setGantryId(gantryId);
//                    identification.setGantryName(travel.getPointName());
//                    if(travel.getGantryId().compareTo(startGantryId)==0){
//                        identification.setEnExFlag("1");
//                    }else if(travel.getGantryId().compareTo(endGantryId)==0){
//                        identification.setEnExFlag("2");
//                    }
//                    //查询mysql 门架基础信息表 T_GANTRY_MOUNTING
//                    jdbcUtil.mysqlUtile(pointType,gantryId,identification);
//                    identification.setProvinceId(provinceId);
//                    identification.setBillMile("**.*");
//                    identification.setLat(travel.getPoint().get(0).getLat()+"");
//                    identification.setLng(travel.getPoint().get(0).getLon()+"");
//
//                    identification.setBdRecStatus("0");
//                    identification.setEtcRecStatus("0");
//                    identification.setPicUrl("http://wwwwww");
//                    identification.setReceivableAmount("***.**");
//                    identification.setDiscountAmount("*.**");
//                    identification.setBillAmount("***.**");
//                    list.add(identification);
//                }
//
//            }else {
//                List<EsDataIncomplete> incompletesData = employeeRepository.findByVehicleNoAndTravelId(vehPlate, queryValue1);
//                List<EsDataIncomplete> sortList = incompletesData.stream().sorted(Comparator.comparing(EsDataIncomplete::getTansTime)).collect(Collectors.toList());
//                if(sortList.size()>0&&sortList!=null){
//
//                    String startGantryId = sortList.get(0).getGantryId();
//                    rtData.setIsOnline("0");
//                    for (EsDataIncomplete incomplete : incompletesData) {
//                        RouteIdentificationData.Identification identification = new RouteIdentificationData.Identification();
//                        identification.setTransTime(incomplete.getTansTime());
//                        identification.setEndLat(incomplete.getTravelId());
//                        identification.setVehStatus("0");
//                        int pointType = incomplete.getGantryType();
//                        identification.setPointType(pointType+"");
//                        String gantryId = incomplete.getGantryId();
//                        identification.setGantryId(gantryId);
//                        identification.setGantryName(incomplete.getPointName());
//                        if(incomplete.getGantryId().compareTo(startGantryId)==0){
//                            identification.setEnExFlag("1");
//                        }
//                        //查询mysql 门架基础信息表 T_GANTRY_MOUNTING
//                        jdbcUtil.mysqlUtile(pointType,gantryId,identification);
//                        identification.setProvinceId(provinceId);
//                        identification.setBillMile("**.*");
//                        identification.setLat(incomplete.getPoint().get(0).getLat()+"");
//                        identification.setLng(incomplete.getPoint().get(0).getLon()+"");
//
//                        identification.setBdRecStatus("0");
//                        identification.setEtcRecStatus("0");
//                        identification.setPicUrl("http://wwwwww");
//                        identification.setReceivableAmount("***.**");
//                        identification.setDiscountAmount("*.**");
//                        identification.setBillAmount("***.**");
//                        list.add(identification);
//                    }
//                    rtData.setEndLatArray(list);
//                    ObjectMapper mapper = new ObjectMapper();
//                    try {
//                        String value = mapper.writeValueAsString(rtData);
//                        return value;
//                    } catch (JsonProcessingException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//            }
        }else if (queryType.equals("1")){
            String queryValue2 = json.getString("queryValue2");
            String startTime =DateUtil.TtoTime(queryValue1);
            String endTime = DateUtil.TtoTime(queryValue2);
            String exStationId = json.getString("exStationId");
            //不查询计费信息
            if(feeFlag==null||feeFlag.equals("0")){
                List<EsDataStrip> timeBetween = employeeRepository.findByVehicleNoAndVehicleColorAndTansTimeBetween(vehPlate, vehPlateColor, startTime, endTime);
                if(timeBetween.size()>0&&timeBetween!=null){
                    EsDataStrip esDataStrip = timeBetween.get(timeBetween.size() - 1);
                    String travelId = esDataStrip.getTravelId();
                    List<EsDataStrip> travelData = employeeRepository.findByVehicleNoAndVehicleColorAndTravelId(vehPlate, vehPlateColor, travelId);
                    List<EsDataStrip> sortData = travelData.stream().sorted(Comparator.comparing(EsDataStrip::getTansTime)).collect(Collectors.toList());
                    if(sortData.size()>0&&sortData!=null){
                        startTime = sortData.get(0).getTansTime();
                        endTime=sortData.get(sortData.size()-1).getTansTime();
                        System.out.println("***********完整行程重新抓取开始结束时间***********");
                    }
                }else {
                    List<EsDataIncomplete> incompleteData = employeeRepository.findByVehicleNoAndTansTimeBetweenAndVehicleColor(vehPlate, startTime, endTime, vehPlateColor);
                    if(incompleteData.size()>0&&incompleteData!=null){
                        EsDataIncomplete incomplete = incompleteData.get(incompleteData.size() - 1);
                        List<EsDataIncomplete> incompleteSort = employeeRepository.findByVehicleNoAndTravelId(vehPlate, incomplete.getTravelId())
                                .stream().sorted(Comparator.comparing(EsDataIncomplete::getTansTime)).collect(Collectors.toList());
                        if(incompleteSort.size()>0&&incompleteSort!=null){
                            startTime = incompleteSort.get(0).getTansTime();
                            System.out.println("***********不完整行程重新抓取起始时间***********");
                        }
                    }

                }
                //对list进行数据添加
                esQueryUtil.esQuery(vehPlate, vehPlateColor,startTime,endTime,list,rtData,exStationId);

                rtData.setEndLatArray(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(rtData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
